//通过这个类实现spring与struts的整合，提高重用性
package com.sy.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.sy.util.Constants;
import com.sy.util.ConvertUtil;

public class BaseAction extends DispatchAction{
//日志
	protected final Log log = LogFactory.getLog(getClass());

	@SuppressWarnings("unused")
	private static final Long defaultLong = null;
	
	static {
//		ConvertUtils.register(new CurrencyConverter(), Double.class);
//		ConvertUtils.register(new DateConverter(), Date.class);
//		ConvertUtils.register(new DateConverter(), String.class);
//		ConvertUtils.register(new LongConverter(defaultLong), Long.class);
//		ConvertUtils.register(new IntegerConverter(defaultLong), Integer.class);
	}

//读取spring配置文件的getBean方法
	public Object getBean(String name) {

		ApplicationContext ctx = WebApplicationContextUtils
				.getRequiredWebApplicationContext(servlet.getServletContext());
		return ctx.getBean(name);
	}

//对象属性转换
	protected Object convert(Object o) throws Exception {
		return ConvertUtil.convert(o);
	}

//对象属性列转换
	protected Object convertLists(Object o) throws Exception {
		return ConvertUtil.convertLists(o);
	}
	
//spring读取Action信息	
	public ActionMessages getMessages(HttpServletRequest request) {
		ActionMessages messages = null;
		HttpSession session = request.getSession();

		if (request.getAttribute(Globals.MESSAGE_KEY) != null) {
			messages = (ActionMessages) request
					.getAttribute(Globals.MESSAGE_KEY);
			saveMessages(request, messages);
		} else if (session.getAttribute(Globals.MESSAGE_KEY) != null) {
			messages = (ActionMessages) session
					.getAttribute(Globals.MESSAGE_KEY);
			saveMessages(request, messages);
			session.removeAttribute(Globals.MESSAGE_KEY);
		} else {
			messages = new ActionMessages();
		}

		return messages;
	}

//spring读取action的属性映射
	public String getActionMethodWithMapping(HttpServletRequest request,
			ActionMapping mapping) {

		return mapping.getParameter();
	}
	
//spring读取Action指向
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String actionMethod = getActionMethodWithMapping(request, mapping);
		@SuppressWarnings("unused")
		String errorMsg = "";
		
		return dispatchMethod(mapping, form, request, response,
				actionMethod);
	
/*		if(!mapping.getPath().equals("/login")&&!mapping.getPath().equals("/logout")){
		if(request.getSession().getAttribute(Constants.GLOBAL_USER)==null){
			
			errorMsg=Constants.ERRORMSG_SESSIONVALIDATE;
			request.setAttribute(Constants.ERROR_MSG, errorMsg);
			return mapping.findForward("error");
		}
		} 
		if (actionMethod != null) {
			try {
				
				return dispatchMethod(mapping, form, request, response,
						actionMethod);
			} catch (Exception e) {
				log.debug(e);
				if (e.getCause() != null) {

					if (e.getCause() instanceof DataAccessException) {
 
						//errorMsg = "Data Access Error";
						errorMsg = Constants.ERRORMSG_DATAERROR;
						request.setAttribute(Constants.ERROR_MSG, errorMsg);
						return mapping.findForward("error");
					} else if (e.getCause() instanceof NestableRuntimeException) {

						//errorMsg = "Data Hibernate Runtime Error";
						errorMsg = Constants.ERRORMSG_DATAERROR;
						request.setAttribute(Constants.ERROR_MSG, errorMsg);
						return mapping.findForward("error");
					} else if(e.getCause() instanceof  SQLException){
						//errorMsg = "  SQLSERVER   QUERY  Error";
						errorMsg = Constants.ERRORMSG_DATABASEERROR;
						request.setAttribute(Constants.ERROR_MSG, errorMsg);
						return mapping.findForward("error");
						
					}else if (e.getCause() instanceof RuntimeException) {

						//errorMsg = "Run Error";
						errorMsg = Constants.ERRORMSG_SERVERERROR;
						request.setAttribute(Constants.ERROR_MSG, errorMsg);
						return mapping.findForward("error");
					}  
				} else {
					if (e instanceof DataAccessException) {

						//errorMsg = "Data Access Error";
						errorMsg = Constants.ERRORMSG_DATAERROR;
						request.setAttribute(Constants.ERROR_MSG, errorMsg);
						return mapping.findForward("error");
					} else if (e instanceof RuntimeException) {
						errorMsg = e.getMessage();
						if(errorMsg==null)
							if(e instanceof java.lang.NullPointerException)
								//errorMsg="NULL  DATA   ERROR��";
								errorMsg = Constants.ERRORMSG_NULLDATAERROR;
						request.setAttribute(Constants.ERROR_MSG, errorMsg);
						return mapping.findForward("error");
					}

				}
			}

		}

		return super.execute(mapping, form, request, response);
*/	}
	
//spring读取ActionForm
	protected ActionForm getActionForm(ActionMapping mapping,
			HttpServletRequest request) {
		ActionForm actionForm = null;

		// Remove the obsolete form bean
		if (mapping.getAttribute() != null) {
			if ("request".equals(mapping.getScope())) {
				actionForm = (ActionForm) request.getAttribute(mapping
						.getAttribute());
			} else {
				HttpSession session = request.getSession();
				actionForm = (ActionForm) session.getAttribute(mapping
						.getAttribute());
			}
		}

		return actionForm;
	}
	
//spring读取config配置文件映射
	@SuppressWarnings("unchecked")
	public Map getConfiguration() {
		Map config = (HashMap) getServlet().getServletContext().getAttribute(
				Constants.CONFIG);

		// so unit tests don't puke when nothing's been set
		if (config == null) {
			return new HashMap();
		}

		return config;
	}
	
//删除FormBean
	protected void removeFormBean(ActionMapping mapping,
			HttpServletRequest request) {
		// Remove the obsolete form bean
		if (mapping.getAttribute() != null) {
			if ("request".equals(mapping.getScope())) {
				request.removeAttribute(mapping.getAttribute());
			} else {
				HttpSession session = request.getSession();
				session.removeAttribute(mapping.getAttribute());
			}
		}
	}
	
//修改FormBean
	protected void updateFormBean(ActionMapping mapping,
			HttpServletRequest request, ActionForm form) {
		// Remove the obsolete form bean
		if (mapping.getAttribute() != null) {
			if ("request".equals(mapping.getScope())) {
				request.setAttribute(mapping.getAttribute(), form);
			} else {
				HttpSession session = request.getSession();
				session.setAttribute(mapping.getAttribute(), form);
			}
		}
	}
}